package com.iaz.tech.tools.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 時間
 * @author Ale
 *
 */
public class DateTimeUtils {
    private static int[] days={31,0,31,30,31,30,31,31,30,31,30,31};
    public static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    /**
     *月份的第一天
     * @param date
     * @return
     */
    public static Date getFirstDayOfMonth(Date date){
        Calendar ca=Calendar.getInstance();
        ca.setTime(date);
        ca.set(Calendar.DAY_OF_MONTH,1);
        return ca.getTime();
    }
    /**
     * 月份的最后一天
     * @param date
     * @return
     */
    public static Date getLastDayofMonth(Date date){
        Calendar ca=Calendar.getInstance();
        ca.setTime(date);
        if(ca.get(Calendar.MONTH)==1){
            ca.set(Calendar.MONTH,2);
            ca.set(Calendar.DAY_OF_MONTH,1);
            ca.add(Calendar.DAY_OF_YEAR,-1);
            ca.set(Calendar.HOUR_OF_DAY,23);
            ca.set(Calendar.MINUTE,59);
            ca.set(Calendar.SECOND,59);
            return ca.getTime();
        }
        ca.set(Calendar.DAY_OF_MONTH,days[ca.get(Calendar.MONTH)]);
        ca.set(Calendar.HOUR_OF_DAY,23);
        ca.set(Calendar.MINUTE,59);
        ca.set(Calendar.SECOND,59);
        return ca.getTime();
    }
    /**
     * 年份的第一天
     * @param date
     * @return
     */
    public static Date getFirstDayOfYear(Date date){
        Calendar ca=Calendar.getInstance();
        ca.setTime(date);
        ca.set(ca.get(Calendar.YEAR),0,1);
        return ca.getTime();
    }
    /**
     * 年份的最后一天
     * @param date
     * @return
     */
    public static Date getLastDayOfYear(Date date){
        Calendar ca=Calendar.getInstance();
        ca.setTime(date);
        ca.set(ca.get(Calendar.YEAR),11,31,23,59,59);
        return ca.getTime();
    }
    
    /**
     * 获得当前天的00:00:00 
     * @param date
     * @return
     */
    public static Date getFristHourOfDay(Date date ){
    	  Calendar ca=Calendar.getInstance();
          ca.setTime(date);
          ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH ), ca.get(Calendar.DAY_OF_MONTH),0,0,0);//       11,31,23,59,59);
          return ca.getTime();
    }
    
    public static Date getLastHourOfDay(Date date ){
  	  Calendar ca=Calendar.getInstance();
        ca.setTime(date);
        ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH ), ca.get(Calendar.DAY_OF_MONTH),23,59,59);//       11,31,23,59,59);
        return ca.getTime();
    }
    
    public static int getDateField(Date date,int field){
        Calendar ca=Calendar.getInstance();
        ca.setTime(date);
        return ca.get(field);
    }
    
    public static void main(String[] args){
        Date dt=new Date();
        System.out.println(getLastDayOfYear(dt));
        System.out.println(getLastDayofMonth(dt));
        System.out.println(getDateField(dt, 1));
        System.out.println(dt);
        System.out.println(getFristHourOfDay(dt));
        System.out.println(getLastHourOfDay(dt));
        System.out.println("----------------------");
        List<Date> dates = UtilTools.getWeekDateForVeryDay( 10);
        for( int i =0; i < dates.size(); i++ ){
        	System.out.println( dates.get( i ));
        }
        
        /* 测试方法 */
        String targFile = "/pdf/report_1644915068227.docx";
        System.out.println( "1:" + UtilTools.getFileIdFromPath( targFile ) + ":DU" );
        
        String targFile1 = "/pdf/report_16449150682\\27.docx";
        System.out.println( "2:" + UtilTools.getFileIdFromPath( targFile1 ) + ":DU" );
        
        String targFile2 = "/pdf/report_16449150682\\27.doc";
        System.out.println( "3:" + UtilTools.getFileIdFromPath( targFile2 ) + ":DU" );
        
        String targFile3 = "/pdf/report_16449150682\\27.DOCX";
        System.out.println( "4:" + UtilTools.getFileIdFromPath( targFile3 ) + ":DU" );
        
        String targFile4 = "/pdf/report_1644915068227.DOC";
        System.out.println( "5:" + UtilTools.getFileIdFromPath( targFile4 ) + ":DU" );
        
    }
}
